package net.lzzy.demo3_5.service;

import net.lzzy.demo3_5.repository.CommentRepository;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.xml.stream.events.Comment;
import java.awt.print.Pageable;
import java.util.List;
import java.util.Optional;

@Service
public class CommentService {

    public List<Comment> selectAll(){
        return commentRepositoy.findAll();
    }

    public void selectById(Integer id){
        Optional<Comment> comment = commentRepository.findById(id);
        if(comment.isPresent())
            System.out.println(comment.get());
        else

            System.out.println("没找到");
    }
    public void delById(Integer id){
        commentRepository.deleteById(id);
    }
    public Comment addComment(String content,String author,Integer id){
        Comment comment= new Comment();
        comment.setAuthor(author);
        comment.setContent(content);
        comment.setAID(aid);
        return (comment) CommentRepository.save(comment);
    }
    public List<Comment>selectAllSortBy(String property){
        return commentRepository.findAll(sort.by(sort.Direction.ASC,property));
    }

    public List<Comment> selectAllPage(int page,int size){
        Pageable pageable= PageRequest.of(page,size);
        return commentRepository.findAll(pageable).getContent();
    }
    public List<Comment> selectByExample(String author){
        Comment comment=new Comment();
        comment.setAuthor(author);
        Example<Comment>commentExample=Example.of(Comment);
        ExampleMatcher matcher=ExampleMatcher.matching(),withMatcher(propery:"author")
        Example<comment> commentExample = Example.of(comment,master);
        return commentRepostory.findAll(commentExample);
    }
}
